package com.shuoyue.ycgk.views.dialog;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.entity.AppVersionInfo;
import com.shuoyue.ycgk.ui.main.LauncherActivity;
import com.shuoyue.ycgk.utils.MyRichTextHelper;
import com.shuoyue.ycgk.utils.XToast;
import com.tencent.bugly.crashreport.BuglyLog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;


/**
 * 开屏广告Dialog
 * Created by Forty'7 on 2019/8/17
 */

public class NewVersionDialog extends Dialog {
    @BindView(R.id.info)
    TextView info;
    @BindView(R.id.download_progress)
    ProgressBar downloadProgress;
    @BindView(R.id.progressText)
    TextView progressText;
    @BindView(R.id.download)
    Button download;
    @BindView(R.id.colose)
    ImageView colose;
    private Activity mContext;

    AppVersionInfo versionInfo;
    int localVersion;

    public NewVersionDialog(@NonNull Activity context) {
        super(context, R.style.dialog);
        this.mContext = context;
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = View.inflate(context, R.layout.dialog_new_version, null);
        setContentView(view);
        ButterKnife.bind(this, view);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.CENTER);

    }

    public void show(AppVersionInfo versionInfo) {
        if (versionInfo == null) {
            return;
        }
        this.versionInfo = versionInfo;
        MyRichTextHelper.showRich(mContext, TextUtils.isEmpty(versionInfo.getBrief()) ? "暂无版本说明" : versionInfo.getBrief(), info);
        super.show();
    }

    public void show(AppVersionInfo versionInfo, int localversion) {
        if (versionInfo == null) {
            return;
        }
        this.versionInfo = versionInfo;
        this.localVersion = localversion;

        MyRichTextHelper.showRich(mContext, TextUtils.isEmpty(versionInfo.getBrief()) ? "暂无版本说明" : versionInfo.getBrief(), info);
        if (localversion < versionInfo.getLowest()) {
            setCancelable(false);
        }


        super.show();
    }

    public void changeProgerss(int progress) {
        downloadProgress.setProgress(progress);
        progressText.setText(progress + "%");
    }

    @OnClick({R.id.download, R.id.colose})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.download:
                if (downloadCallback != null) {

                    String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE};

                    if (EasyPermissions.hasPermissions(mContext, permissions)) {
                        downloadCallback.download(versionInfo.getDownloadUrl());
                        download.setVisibility(View.GONE);
                        progressText.setVisibility(View.VISIBLE);
                    } else {
                        BuglyLog.i("权限", "更新申请存储权限");
                        EasyPermissions.requestPermissions(mContext,
                                "需要用到文件访问权限", 1, permissions);
                    }


                }
                break;
            case R.id.colose:
                if (localVersion != 0 && localVersion < versionInfo.getLowest()) {
                    XToast.toast(mContext, "当前版本已弃用，请更新后使用");
                } else {
                    SPUtils.setPrefInt(mContext, "main_version_code", versionInfo.getVersion());
                    dismiss();
                }
                break;
        }
    }

    DownloadCallback downloadCallback;

    public void setDownloadCallback(DownloadCallback downloadCallback) {
        this.downloadCallback = downloadCallback;
    }

    public interface DownloadCallback {
        void download(String url);
    }
}
